//menu function
define(["app"], function (app) {
    app.run(["$state", "$rootScope", "$filter", "$templateRequest", "$compile", "config", function($state, $rootScope, $filter, $templateRequest, $compile, config){

        var menuTemp = config.program.find(function(p) { return p.name == 'menu'; }).templateUrl;
        $rootScope.menuState = '';
        $rootScope.isLoadMenu = false;
        $rootScope.isShowMenu = false;
        //載入menu
        $rootScope.loadMenu = function(menu){
            if(!$rootScope.isLoadMenu){
                $templateRequest(menuTemp).then(function(respose){
                    var menuDiv = document.getElementById('menu');
                    var menuScope = menuController($rootScope.$new(true), menu);
                    angular.element(menuDiv).append($compile(respose)(menuScope)).removeClass('menu-hide');
                    $rootScope.isLoadMenu = true;
                    showMenu(menu);
                });
            } else {
                showMenu(menu);
            }
        };

        //開啟menu
        $rootScope.permissions = [];
        $rootScope.showMenu = showMenu;
        $rootScope.menuList = [];
        function showMenu(menu){
            if(menu){
                $rootScope.menuList = menu;
            }

            if(checkMenu()){
                $rootScope.isShowMenu = true;
                $rootScope.menuState = 'is-menu-showed';
            }
        }

        function checkMenu(){
            if($rootScope.menuList && $rootScope.menuList.length>0){
                var menuItem_count = 0;
                $rootScope.menuList.forEach(function(menuItem){
                    if(menuItem.isShow){
                        menuItem_count++;
                    }
                });

                return menuItem_count>1;
            } else {
                return false;
            }
        }

        //關閉menu
        $rootScope.hideMenu = function(){
            $rootScope.isShowMenu = false;
            $rootScope.menuState = '';
        };

        function menuController(scope){
            scope.menuClick = function(item, event){
                var paramets;
                if(item.isReport == true){
                    paramets = {data:{is_report:true, report_name:item.title, isOutsideReport:item.isOutsideReport}};
                }
                //20220119 add by Liuty for M#0103660:是否查看報表
                if (item.is_view == true) {
                    paramets = { data: { is_view: true } };
                }
				if($rootScope.menuClick){
					$rootScope.menuClick(item.name, item);
				} else {
					$rootScope.changeProgram(item.name, paramets);
				}
			};
			scope.showAllMenu = function(){
				$rootScope.showSelect({
					label : function(item){
						return $filter('translate')(item.title);
					},
					code : 'name',
					//selectCode : item.column_name,
					list : $filter('filter')($rootScope.menuList, {isShow:true}),
					confirm : function(selectItem, dialog){
						dialog.hide();
						scope.menuClick(selectItem);
					}
				});
			}
			return scope;
		}

        $rootScope.currentState = '';
        $rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
            $rootScope.currentState = toState.name;
        });

    }]);
    //sub-menu
    app.run(["$state", "$rootScope", "$filter", "$templateRequest", "$compile", "$timeout", "config", 'RouterExtendProvider', function($state, $rootScope, $filter, $templateRequest, $compile, $timeout, config, $RouterExtendProvider){
        //載入側邊欄
        var sideMenuTemp = config.program.find(function(p) { return p.name == 'sideMenu'; }).templateUrl,
            sideMenuScope;
        $rootScope.loadSideMenu = function(){
            return new Promise((success, error) => {
                if(!$rootScope.isLoadSideMenu){
                    $templateRequest(sideMenuTemp).then(function(response){
                        var menuDiv = document.getElementById('side-menu');
                        sideMenuScope = SideMenuController($rootScope.$new(true));
                        sideMenuScope.sideTools = $RouterExtendProvider.GetSideTools();
                        angular.element(menuDiv).append($compile(response)(sideMenuScope));
                        $rootScope.isLoadSideMenu = true;
                        $rootScope.showSideMenu();
                        $timeout(success);
                    });
                } else {
                    $rootScope.showSideMenu();
                    $timeout(success);
                }
            });
        };
        $rootScope.showSideMenu = function(){
            angular.element(document.getElementById('side-menu')).removeClass('menu-hide');
            angular.element(document.getElementById('view-content')).addClass('mode-side');
            $rootScope.isShowSideMenu = true;
            sideMenuScope.modules = $rootScope.menu||[];
        };
        $rootScope.hideSideMenu = function(){
            angular.element(document.getElementById('side-menu')).addClass('menu-hide');
            angular.element(document.getElementById('view-content')).removeClass('mode-side');
            $rootScope.isShowSideMenu = false;
        };
        $rootScope.showSideProgram = function(){
            if(sideMenuScope && sideMenuScope.isShowProgram === false)
				sideMenuScope.isShowProgram = true;
        };
        $rootScope.hideSideProgram = function(){
            if(sideMenuScope && sideMenuScope.isShowProgram === true)
                sideMenuScope.isShowProgram = false;
        };
        function SideMenuController(scope){
            scope.modules = $rootScope.modules||[];
            scope.isShowProgram = false;
            // 20230314 modify by Alan for#130175: 增加手機端顯示判斷
            scope.isPhone = config.cache.device === 'phone';
            scope.home = function(){
                scope.hideSideSubMenu();
                $RouterExtendProvider.home();
            };
            scope.showSideSubMenu = function(moduleItem){
                console.log(moduleItem);
                $rootScope.menuTitle = moduleItem.title;
                $RouterExtendProvider.goToModule(moduleItem, $rootScope.loadSideSubMenu, $rootScope.hideSideSubMenu);
            };
            scope.hideSideSubMenu = function(){
                $rootScope.hideSideSubMenu();
            };
            scope.settings = function(){
                scope.hideSideSubMenu();
                $RouterExtendProvider.triggerSetting();
            };
            scope.myFavourite = function(){
                $rootScope.hideSideSubMenu();
                $rootScope.hideMenu();
                $RouterExtendProvider.myFavourite('main');
            };
            // 20220615 modify by Alan for#112054 : 修改作業搜尋選擇顯示
            scope.openSearchDialog = function(){
                $rootScope.hideSideSubMenu();
                scope.isOpenSearchDialog = true;
                $RouterExtendProvider.openSearchDialog(function() {
                    scope.isOpenSearchDialog = false;
                });
            };
            scope.logout = function(){
                scope.hideSideSubMenu();
                $rootScope.showConfirm($filter('translate')('home.msg.is_logout'),function(dialog){
                    dialog.hide();
                    $RouterExtendProvider.logout();
                });
            };
            return scope;
        }

        // 載入側邊欄清單
        var subMenuTemp = config.program.find(function(p) { return p.name == 'sideSubMenu'; }).templateUrl,
            subMenuScope;
        $rootScope.loadSideSubMenu = function(moduleItem, options){
            if(!$rootScope.isLoadSideSubMenu){
                $templateRequest(subMenuTemp).then(function(response){
                    var menuDiv = document.getElementById('side-sub-menu');
                    response = '<div id="sub-menu-content" class="sub-menu-content" ng-class="[$root.sideSubMenuMode]">'+response;
                    $RouterExtendProvider.addExtendSideMenu(
                        SideSubMenuController($rootScope.$new(true))
                        , response , ''
                        , function(scope, sideMenuTemp, sideExtendTemp){
                            subMenuScope = scope;
                            // sideMenuTemp += '<div class="side-menu-border"><div class="side-menu-border__inner"></div></div>';
                            sideMenuTemp += sideExtendTemp;
                            sideMenuTemp += '<div class="sub-menu-background" ng-click="hideSideSubMenu();"></div></div>';
                            angular.element(menuDiv).append($compile(sideMenuTemp)(scope));//.removeClass('menu-hide');
                            $rootScope.isLoadSideSubMenu = true;
                            $rootScope.showSideSubMenu(options.mode, moduleItem);
                        }
                    );
                });
            } else {
                $rootScope.showSideSubMenu(options.mode, moduleItem);
            }
        };
        $rootScope.showSideSubMenu = function(mode, moduleItem){
            angular.element(document.getElementById('side-sub-menu')).removeClass('menu-hide');
            $rootScope.isShowSideSubMenu = true;
            $rootScope.selectModule = moduleItem;
            $rootScope.sideSubMenuMode = mode;
            //subMenuScope.mode = mode;
        };
        $rootScope.hideSideSubMenu = function(){
            angular.element(document.getElementById('side-sub-menu')).addClass('menu-hide');
            $rootScope.isShowSideSubMenu = false;
            $rootScope.selectModule = {};
            $rootScope.sideSubMenuMode = '';
            //subMenuScope.mode = '';
        };
        function SideSubMenuController(scope){
            //scope.mode = '';
            scope.config = config;
            //scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
            scope.hideSideSubMenu = function(){
                $rootScope.hideSideSubMenu();
            };
            scope.getLogo = function(){
                return {'background-image': 'url("image/PN_home_'+config.setting.lang + '.png")'};
            };
            scope.ChangeProgram = function(menuItem){
                $rootScope.hideSideSubMenu();
                $RouterExtendProvider.goToProgram(menuItem);
            };
            return scope;
        }
    }]);
});
